package com.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.DB;
import com.tools.Constants;

public class C2S8phoneVersion extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:" + Constants.DBNAME);
		DB.connDB();
		super.init();
	}

	/**
	 * 
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[vswb8] whitebox8 get begin");
		System.out.println("版本> 14");
		PrintWriter writer = response.getWriter();
		String versionCode = null;
		String versionName = null;
		String packageName = null;
		String isFirst = null;
		String encoding = null;
		String user_id = null;

		while (er.hasMoreElements()) {
			String element = er.nextElement();
			if (element.equals("versionCode")) {
				versionCode = request.getParameterValues("versionCode")[0];
				System.out.println("[vswb8] versionCode:" + versionCode);
			}
			if (element.equals("versionName")) {
				versionName = request.getParameterValues("versionName")[0];
				System.out.println("[vswb8] versionName:" + versionName);
			}
			if (element.equals("packageName")) {
				packageName = request.getParameterValues("packageName")[0];
				System.out.println("[vswb8] packageName:" + packageName);
			}
			if (element.equals("isFirst")) {
				isFirst = request.getParameterValues("isFirst")[0];
				System.out.println("[vswb8] isFirst:" + isFirst);
			}
			if (element.equals("encoding")) {
				encoding = request.getParameterValues("encoding")[0];
				System.out.println("[vswb8] encoding:" + encoding);
			}
			if (element.equals("user_id")) {
				user_id = request.getParameterValues("user_id")[0];
				System.out.println("[vswb8] user_id:" + user_id);
			}
		}

		if ((versionCode != null) && (versionName != null) && (packageName != null) && (user_id != null) && (encoding != null)) {
			DB.exeCmd("update userinfo set versionCode='" + versionCode + "' , versionName='" + versionName + "' , packageName='" + packageName + "' , encoding='" + encoding + "' where user_id='" + user_id + "';");
			System.out.println("update user_id:" + user_id + " client info ok");
			ResultSet rs = DB.select("select clientVersionCode from config;");
			String clientVersionCode = new String();
			try {
				while (rs.next()) {
					clientVersionCode = rs.getString("clientVersionCode");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

			if (Integer.valueOf(versionCode) < Integer.valueOf(clientVersionCode)) {
				writer.println("尊贵的用户，你当前使用的易物版本是"+versionCode+"版，最新的易物版本是"+clientVersionCode+"版，请点击确定升级至最新版，享受易物带给你的更多功能。所有个人资料都将保留。鄂http://115.238.95.2:8089/yiwu.apk");
			}else{
				
			}
		}
	}
}
